﻿using System;
using System.IO;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.Mail;
using CE.User;
using CE.Global;
using CE.Security;
using CE.InputOut;
using CE.Database;
using CE.CarInsurance;
using CXDecryption;

public partial class carinsurance_fleet_FleetCompany : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            LoadAgentList();
            ClearFields();
            txtCompanyId.Text = "0";
            if (Request.QueryString["CompanyId"] != null)
            {
                txtCompanyId.Text = Request.QueryString["CompanyId"].ToString(); 
                LoadCompanyDetails();
                if (txtCompanyId.Text == "0" && Session[CE.Global.General.UserRole].ToString() == "100")
                {
                    ddlAgentList.Text = Session[CE.Global.General.UserId].ToString();
                }
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            Page.Validate("company");
            if (Page.IsValid)           
            {
                Int64 _result = -1;
                FleetCompany _fleetcompany = new FleetCompany();
                FleetCompanyDB _fleetcompanydb = new FleetCompanyDB();

                _fleetcompany.CompanyId = Convert.ToInt64(txtCompanyId.Text.Trim());
                _fleetcompany.CompanyName = Server.HtmlEncode(txtCompanyName.Text.Trim());
                _fleetcompany.ContactPerson = Server.HtmlEncode(txtContactPerson.Text.Trim());
                _fleetcompany.CompanyAddress = Server.HtmlEncode(txtAddress.Text.Trim());
                _fleetcompany.SubDistrict = Server.HtmlEncode(txtSubDistrict.Text.Trim());
                _fleetcompany.District = Server.HtmlEncode(txtDistrict.Text.Trim());
                _fleetcompany.Province = Server.HtmlEncode(txtProvince.Text.Trim());
                _fleetcompany.PostalCode = Server.HtmlEncode(txtPostalCode.Text.Trim());
                _fleetcompany.ContactNo1 = Server.HtmlEncode(txtContactNo1.Text.Trim());
                _fleetcompany.ContactNo2 = Server.HtmlEncode(txtContactNo2.Text.Trim());
                _fleetcompany.Email = Server.HtmlEncode(txtEmailAddress.Text.Trim());
                _fleetcompany.AgentId = Convert.ToInt64(ddlAgentList.Text);
                _fleetcompany.AdminId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                _fleetcompany.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                _fleetcompany.Status = true;

                _result = _fleetcompanydb.Insert(_fleetcompany);
                if (_result > 0)
                {
                    ClearFields();
                    if (_fleetcompany.CompanyId == 0)
                    {
                        lblMessage.Text = "Successfully Saved!";
                        lblMessage.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (_fleetcompany.CompanyId > 0)
                    {
                        lblMessage.Text = "Successfully Updated!";
                        lblMessage.ForeColor = System.Drawing.Color.Green;
                    }
                    _fleetcompany = null;
                    _fleetcompanydb = null;
                }
                else
                {
                    if (_fleetcompany.CompanyId == 0)
                    {
                        lblMessage.Text = "Insert Error!";
                        lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                    else if (_fleetcompany.CompanyId > 0)
                    {
                        lblMessage.Text = "Update Error!";
                        lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message.ToString();
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void btnView_Click(object sender, EventArgs e)
    {
        Response.Redirect("FleetCompanyList.aspx");
    }

    private void LoadAgentList()
    {
        try
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dt = new DataTable("tblList");
            string _spName = "sprocCXCRMGetAgentList";
            SortedList paraList = new SortedList();
            paraList.Add("@AgentID", Session[General.UserId].ToString());
            _dt = _db.GetDataTableBySP(_spName, paraList);
            ddlAgentList.Items.Clear();
            if (_dt != null && _dt.Rows.Count > 0)
            {
                ddlAgentList.DataSource = _dt;
                ddlAgentList.DataTextField = "FULLNAME";
                ddlAgentList.DataValueField = "CEID";
                ddlAgentList.DataBind();
            }
            ddlAgentList.Items.Add(new ListItem("Direct Sale", "0"));
            ddlAgentList.Text = "0";
        }
        catch (Exception Ex)
        {
        }
    }

    private void ClearFields()
    {
        lblMessage.Text = "";
        txtCompanyId.Text = "0";
        txtCompanyName.Text = "";
        txtContactPerson.Text = "";
        txtAddress.Text = "";
        txtSubDistrict.Text = "";
        txtDistrict.Text = "";
        txtProvince.Text = "";
        txtPostalCode.Text = "";
        txtContactNo1.Text = "";
        txtContactNo2.Text = "";
        txtEmailAddress.Text = "";
        ddlAgentList.Text = "0";
    }

    private void LoadCompanyDetails()
    {
        DataSet _dsFleetCompany = new DataSet();
        FleetCompanyDB _fleetcompanydb = new FleetCompanyDB();
        Int64 CompanyId = Convert.ToInt64(txtCompanyId.Text.Trim());
        _dsFleetCompany = _fleetcompanydb.GetFleetCompany(CompanyId);
        if (_dsFleetCompany != null && _dsFleetCompany.Tables.Count > 0 && _dsFleetCompany.Tables[0].Rows.Count > 0)
        {
            txtCompanyName.Text = _dsFleetCompany.Tables[0].Rows[0]["CompanyName"].ToString();
            txtContactPerson.Text = _dsFleetCompany.Tables[0].Rows[0]["ContactPerson"].ToString();
            txtAddress.Text = _dsFleetCompany.Tables[0].Rows[0]["CompanyAddress"].ToString();
            txtSubDistrict.Text = _dsFleetCompany.Tables[0].Rows[0]["SubDistrict"].ToString();
            txtDistrict.Text = _dsFleetCompany.Tables[0].Rows[0]["District"].ToString();
            txtProvince.Text = _dsFleetCompany.Tables[0].Rows[0]["Province"].ToString();
            txtPostalCode.Text = _dsFleetCompany.Tables[0].Rows[0]["PostalCode"].ToString();
            txtContactNo1.Text = _dsFleetCompany.Tables[0].Rows[0]["ContactNo1"].ToString();
            txtContactNo2.Text = _dsFleetCompany.Tables[0].Rows[0]["ContactNo2"].ToString();
            txtEmailAddress.Text = _dsFleetCompany.Tables[0].Rows[0]["Email"].ToString();
        }
    }

}
